package com.jl.cloud.terminal.api;

import com.alibaba.fastjson.JSONObject;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Enumeration;

/**
 * feign调用时传递header
 *
 * @author chengd
 */
@Slf4j
public class FeignConfigInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return;
        }
        HttpServletRequest request = attributes.getRequest();
        // feign内部调用标识
        String innerKey = "123123";
        requestTemplate.header("123", innerKey);
        String contextSecurity = request.getHeader("123");
        requestTemplate.header("123", contextSecurity);
        Enumeration<String> parameterNames = request.getParameterNames();
        log.info("FEIGN调用请求信息：uri={}, parameters={}", request.getRequestURI(),
                JSONObject.toJSONString(parameterNames));
    }

}
